import { isReferee } from "@/func";
import { Player, system } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
import { GameComponent } from "@sapi-game/gameComponent";
import { Game } from "@sapi-game/main";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { Configdb } from "sapi-pro";
import { isNum } from "sapi-pro/scripts/func";
import { BlockedInCombatConfig } from "../interface";
import { BlockedInCombatMaps } from "../map";
import { BlockedLobbyState } from "../states/lobbyState";

const defaultConfig: BlockedInCombatConfig = {
    mapIndex: 0,
    liveCnt: 3,
};

export class BlockedConfigManager extends GameComponent<BlockedLobbyState> {
    private readonly db = Configdb;
    private readonly configKey = "BlockedInCombat_Config";

    override onAttach(): void {
        this.subscribe(
            Game.events.signClick,
            (t) => {
                if (!isReferee(t.player)) {
                    return t.player.sendMessage("你无权配置此项");
                }
                system.run(() => {
                    this.showConfigForm(t.player);
                });
            },
            {
                dimensionId: DimensionIds.Overworld,
                loc: { x: 872, y: -51, z: 946 },
                clickInterval: 5,
            }
        );
    }

    getConfig() {
        return (this.db.getJSON(this.configKey) ??
            defaultConfig) as BlockedInCombatConfig;
    }

    private setConfig(config: BlockedInCombatConfig) {
        this.validateConfig(config);
        this.db.setJSON(this.configKey, config);
    }

    private validateConfig(config: BlockedInCombatConfig) {
        if (!config) throw new Error("config为空");
        if (config.mapIndex == undefined || config.liveCnt == undefined) {
            throw new Error("有空项目");
        }
        if (!isNum(config.liveCnt)) {
            throw new Error("生命必须为数字类型");
        }
        if (!isNum(config.mapIndex)) {
            throw new Error("地图index必须为数字类型");
        }
    }

    private showConfigForm(player: Player) {
        const config = this.getConfig();
        const form = new ModalFormData()
            .title("惊天旷工团配置")
            .dropdown(
                "地图",
                BlockedInCombatMaps.map((m) => m.name),
                { defaultValueIndex: config.mapIndex }
            )
            .dropdown("生命数", ["1", "2", "3", "4", "5"], {
                defaultValueIndex: config.liveCnt - 1,
            });
        //展示
        form.show(player).then((ans) => {
            if (!ans.formValues) return;
            if (!this.isAttached) return;
            try {
                this.setConfig({
                    mapIndex: ans.formValues[0] as number,
                    liveCnt: (ans.formValues[1] as number) + 1,
                });
                player.sendMessage("配置成功");
            } catch (err) {
                player.sendMessage(
                    err instanceof Error ? err.message : "未知错误"
                );
            }
        });
    }
}
